//
//  FCSegmentNavView.h
//  FastCall
//
//  Created by Carlson Lee on 2017/12/8.
//  Copyright © 2017年 XiaoNiu Ltd. All rights reserved.
//

#import "FCSegmentNavView.h"

@implementation FCSegmentNavView

//子类重写方法
- (void)segmentScrollContentOffset:(CGFloat)offsetX
{
    CGFloat f_w = self.bounds.size.width;
    NSInteger num = self.items.count;
    NSInteger idx = self.index;
    if(offsetX<0 || offsetX>f_w*(num-1))return;
    CGRect o_rt = CGRectZero;
    CGRect d_rt = CGRectZero;
    UIButton* o_btn = self.items[idx];
    UIButton* d_btn;
    static CGFloat s_f = 0;
    BOOL isLeft;
    NSInteger tag = idx;
    if(offsetX > f_w*idx){
        isLeft = YES;
        if(idx!=num-1)d_btn = self.items[idx+1];
        if(offsetX >= f_w*(idx+1)){
            tag = idx+1;
        }
    }else{
        isLeft = NO;
        if(idx)d_btn = self.items[idx-1];
        if(offsetX <= f_w*(idx-1)){
            tag = idx-1;
        }
    }
    
    o_rt = o_btn.frame;
    if(d_btn)d_rt = d_btn.frame;
    CGFloat f_des = fabsf(d_rt.origin.x-o_rt.origin.x);
    CGFloat f_x = fabsf(offsetX-f_w*idx);
    CGFloat f_base = f_w/3;
    CGRect lrt = self.tagLine.frame;
    if(f_x<f_base){
        if(isLeft){
            lrt.origin.x = CGRectGetMidX(o_rt)-self.lineSize.width/2 + f_x/f_base*self.lineSize.width/2;
        }else{
            lrt.origin.x = CGRectGetMidX(o_rt)-self.lineSize.width/2 - f_x/f_base*(f_des-self.lineSize.width/2);
        }
        lrt.size.width = self.lineSize.width + f_x/f_base*(f_des-self.lineSize.width);
    }else{
        if(isLeft){
            lrt.origin.x = CGRectGetMidX(o_rt) + (f_x-f_base)/(f_w/3*2)*(f_des-self.lineSize.width/2);
        }else{
            lrt.origin.x = CGRectGetMidX(o_rt) - f_des - (f_x-f_base)/(f_w/3*2)*self.lineSize.width/2;
        }
        lrt.size.width = f_des-(f_x-f_base)/(f_w/3*2)*(f_des-self.lineSize.width);
    }

    self.tagLine.frame = lrt;
    
    if(tag != idx){
        [self selectWithTag:tag];
    }
    s_f = offsetX;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
